//
//  UIView+Size.swift
//  UIView 扩展
//
//  Created by mini on 2025/4/28.
//

import Foundation
import UIKit

private var topToUpViewKey: UInt8 = 0
private var bottomToDownViewKey: UInt8 = 0
private var topToUpViewBGColorKey: UInt8 = 0

extension UIView{
    
    // MARK: - 运行时
    var topToUpView: CGFloat {
        get {
            if let num = objc_getAssociatedObject(self, &topToUpViewKey) as? NSNumber {
                return CGFloat(num.floatValue)
            }
            return 0
        }
        set {
            objc_setAssociatedObject(self, &topToUpViewKey, NSNumber(value: Float(newValue)), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
        
    var bottomToDownView: CGFloat {
        get {
            if let num = objc_getAssociatedObject(self, &bottomToDownViewKey) as? NSNumber {
                return CGFloat(num.floatValue)
            }
            return 0
        }
        set {
            objc_setAssociatedObject(self, &bottomToDownViewKey, NSNumber(value: Float(newValue)), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
        
    var topToUpViewBGColor: UIColor? {
        get {
            return objc_getAssociatedObject(self, &topToUpViewBGColorKey) as? UIColor
        }
        set {
            objc_setAssociatedObject(self, &topToUpViewBGColorKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    // MARK: - 组合
    var leftTop: XY {
        get {
            return XY(self.left, self.top)
        }
        set {
            self.left = newValue.x
            self.top = newValue.y
        }
    }
    
    var leftCenterY: XY {
        get {
            return XY(self.left, self.centerY)
        }
        set {
            self.left = newValue.x
            self.centerY = newValue.y
        }
    }
    
    var leftBottom: XY {
        get {
            return XY(self.left, self.bottom)
        }
        set {
            self.left = newValue.x
            self.bottom = newValue.y
        }
    }
    
    var centerXTop: XY {
        get {
            return XY(self.centerX, self.top)
        }
        set {
            self.centerX = newValue.x
            self.top = newValue.y
        }
    }
    
    var centerXCenterY: XY {
        get {
            return XY(self.centerX, self.centerY)
        }
        set {
            self.centerX = newValue.x
            self.centerY = newValue.y
        }
    }
    
    var centerXBottom: XY {
        get {
            return XY(self.centerX, self.bottom)
        }
        set {
            self.centerX = newValue.x
            self.bottom = newValue.y
        }
    }
    
    var rightTop: XY {
        get {
            return XY(self.right, self.top)
        }
        set {
            self.right = newValue.x
            self.top = newValue.y
        }
    }
    
    var rightCenterY: XY {
        get {
            return XY(self.right, self.centerY)
        }
        set {
            self.right = newValue.x
            self.centerY = newValue.y
        }
    }
    
    var rightBottom: XY {
        get {
            return XY(self.right, self.bottom)
        }
        set {
            self.right = newValue.x
            self.bottom = newValue.y
        }
    }
    
    var widthHeight: XY {
        get {
            return XY(self.width, self.height)
        }
        set {
            self.width = newValue.x
            self.height = newValue.y
        }
    }
    
    
    // MARK: - 获取高度
    static func fetchHeight(model: Any) -> CGFloat {
        return fetchHeight(model: model, className: NSStringFromClass(self))
    }

    static func fetchHeight(model: Any, className: String) -> CGFloat {
        let strClassName = isStr(className) ? (className) : (NSStringFromClass(self))
        return self.fetchHeight(model: model, className: strClassName, selectorName: "resetCellWithModel:")
    }
    
    static func fetchHeight(model: Any, selectorName: String) -> CGFloat {
        return self.fetchHeight(model: model, className: NSStringFromClass(self), selectorName: selectorName)
    }
    
    static func fetchHeight(model: Any, className: String?, selectorName: String) -> CGFloat {
        var dic: NSMutableDictionary?
        if dic == nil {
            dic = NSMutableDictionary()
        }
        var strClassName = className
        if(!isStr(strClassName)){
            strClassName = String(reflecting: self)
        }
        var cell = dic!.object(forKey: strClassName!) as? UIView
        if cell == nil {
            if let classType = NSClassFromString(strClassName!) as? UIView.Type {
                cell = classType.init()
                dic!.setObject(cell!, forKey: strClassName! as NSCopying)
            }
        }
        if let cell = cell {
            _ = GlobalMethod.performSelector(selectorName: selectorName, delegate: cell, object: model, isHasReturn: false)
        }
        return cell!.height
    }
    
    static func fetchHeight(model: Any, par: Any, className: String?, selectorName: String) -> CGFloat {
        var dic: NSMutableDictionary?
        if dic == nil {
            dic = NSMutableDictionary()
        }
        var strClassName = className
        if(!isStr(strClassName)){
            strClassName = String(reflecting: self)
        }
        var cell = dic!.object(forKey: strClassName!) as? UIView
        if cell == nil {
            if let classType = NSClassFromString(strClassName!) as? UIView.Type {
                cell = classType.init()
                dic!.setObject(cell!, forKey: strClassName! as NSCopying)
            }
        }
        cell!.perform(Selector(selectorName), with: model, with: par)
        return cell!.height
    }
}
